﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace ChatForum.Client
{
	public partial class MyPost : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				categoryDD.DataSource = ServiceHelper.Instance.ForumService.GetAllCategory();
				categoryDD.DataTextField = "Name";
				categoryDD.DataValueField = "ID";
				categoryDD.DataBind();

				LoadAllMyPost();
			}
		}
		private void LoadAllMyPost()
		{
			myPostList.DataSource = ServiceHelper.Instance.ForumService.GetPostbyCreator(User.Identity.Name, false).OrderByDescending(p=> p.CreatedDate).Select(p => new MyPostView(p));
			myPostList.DataBind();
		}
		protected void OnCreatePost(object sender, EventArgs e)
		{
			Guid outId = Guid.TryParse(categoryDD.SelectedValue, out outId) ? outId : Guid.Empty;
			Handler.AddPost(txtNewPost.Text, outId);

            //Clearing Input
            txtNewPost.Text = null;
            categoryDD.SelectedIndex = 0;

			LoadAllMyPost();
		}
	}
}